﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Xenta.Utils;
using Xenta.Attributes;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Commerce.Controllers
{
    [Authenticate, InRole("ADMIN")]
    public sealed class StockController : ExtController
    {
        #region GET/POST Actions

        [HttpGet, RestoreState]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost, PersistState]
        public ActionResult Set(int itemID,
            string adjustmentMin,
            string adjustmentMax,
            string reserve,
            string exhaustionThreshold,
            string[] flags)
        {
            try
            {
                Svc.ProcessOperation("SetStockEntry", new
                {
                    ItemID = itemID,
                    AdjustmentMin = Svc.Numeric(adjustmentMin),
                    AdjustmentMax = Svc.Numeric(adjustmentMax),
                    Reserve = Svc.Numeric(reserve),
                    ExhaustionThreshold = Svc.Numeric(exhaustionThreshold),
                    Flags = flags.Merge(",")
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Index"));
        }

        [HttpGet, PersistState]
        public ActionResult Delete(int id)
        {
            try
            {
                Svc.ProcessOperation("DeleteStockEntry", new
                {
                    EntityID = id
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Index"));
        }

        #endregion

        #region AJAX Actions

        [HttpPost]
        public ActionResult Grid(int? page,
            int? rows,
            string sord,
            string sidx, 
            string searchTerm)
        {
            page = page.HasValue ? page.Value - 1 : 0;
            rows = rows.HasValue ? rows.Value : 30;
            sord = sord.Equals("desc") ? "Descending" : "Ascending";

            var m = Svc.ProcessOperation("SearchStockEntries", new 
            {
                Term = new
                {
                    Expression = searchTerm
                },
                Index = page * rows,
                Count = rows,
                SortBy = new[] 
                {
                    sidx
                },
                SortDir = sord
            }, new
            {
                DisclosureLevel = "Base"
            });
            var data = new List<Object>();

            foreach(var i in m.Items)
            {
                data.Add(new[] { 
                    JDM.Int32(i.EntityID),
                    JDM.String(i.Item.SKU),
                    Svc.Numeric((decimal)i.AdjustmentMin),
                    Svc.Numeric((decimal)i.AdjustmentMax),
                    Svc.Numeric((decimal)i.ExhaustionThreshold),
                    Svc.Numeric((decimal)i.Reserve),
                    JDM.String(i.Flags)
                });
            }

            return Json(new
            {
                pageIndex = page + 1,
                pageCount = Math.Ceiling(JDM.Double(m.TotalCount) / rows),
                totalCount = JDM.Int64(m.TotalCount),
                data = data.ToArray()
            });
        }

        #endregion
    }
}
